package com.sugou.model.core.base;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.Table;

import org.apache.commons.lang.StringUtils;

import com.sugou.model.core.IdEntity;

/**
 * 公共Long类型ID实体
 * 
 * @version 1.0 
 * @author Hunk.Tong &copy; Ustudy 游学网络
 * <b>Create Time:</b>2013-5-6 下午5:06:21
 */
@MappedSuperclass
public class LongIdEntity implements IdEntity, Serializable {
	private static final long serialVersionUID = 1606348351940221426L;
	
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;

	@Override
	public Serializable getId() {
		return this.id;
	}

	@Override
	public void setId(Serializable id) {
		this.id=(Long) id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@Override
	public int hashCode() {
		String tableName=this.tableName();
		int id=this.id==null?0:this.id.intValue()^(int)(this.id>>>32);
		if(StringUtils.isNotBlank(tableName)){
			return id^tableName.hashCode();
		}else{
			return id^this.entityName().hashCode();
		}
		
	}
	
	@Override
	public boolean equals(Object obj) {
		if(obj==null){
			return false;
		}
		if(!(obj instanceof LongIdEntity)){
			return false;
		}
		LongIdEntity entity=(LongIdEntity) obj;
		if(this.id==null||entity.id==null){
			return false;
		}
		boolean typecheck=false;
		if(StringUtils.isNotBlank(this.tableName())){
			this.tableName().equals(entity.tableName());
		}else{
			typecheck=this.entityName().equals(entity.entityName());
		}
		return (this.id.longValue()==entity.id.longValue())&&typecheck;
	}

	/**
	 * 请大家务必在实体里覆盖toString()
	 */
	@Override
	public String toString() {
		StringBuffer info = new StringBuffer("entity:{");
		info.append("tableName:'").append(this.tableName()).append("'");
		info.append(",entityName:'").append(this.entityName()).append("'");
		info.append(",id:'").append(this.id == null ? "0" : this.id.longValue()).append("'");
		info.append("}");
		return info.toString();
	}
	
	/**
	 * 获取映射配置中的表名，此方法要求必须通过注释映射表名，且指定表名，否则返回空字符串
	 * @return 表名称
	 */
	public String tableName(){
		Table table=this.getClass().getAnnotation(Table.class);
		if(table==null||StringUtils.isBlank(table.name())){
			org.hibernate.annotations.Table htable=this.getClass().getAnnotation(org.hibernate.annotations.Table.class);
			if(htable==null||StringUtils.isBlank(htable.appliesTo())){
				return "";
			}else{
				return htable.appliesTo();
			}
		}else{
			return table.name();
		}
	}
	
	/**
	 * 获取映射配置的对象类型名，此方法要求通过注释映射对象名，且指定对象名，否则返回当前对象类名
	 * @return 对象类型名称
	 */
	public String entityName(){
		Entity entity=this.getClass().getAnnotation(Entity.class);
		if(entity!=null&&StringUtils.isNotBlank(entity.name())){
			return entity.name();
		}else if(this.getClass().getName().indexOf("$")!=-1){
			return this.getClass().getSuperclass().getName();
		}else{
			return this.getClass().getName();
		}
	}
	
}
